using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;

namespace marbletrack
{
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Grid
    {
        public VertexPositionNormalTexture[] pointList;
        public int[] lineListIndices;
        internal int POINTLISTINDEXSIZE;
        protected Texture2D texture;

        VertexDeclaration vertexDeclaration;

        public Grid()
        {
            POINTLISTINDEXSIZE = (int)((Game1.TOTALGRIDSIZE / Game1.SQUARESIZE) * 8 + 4);
            pointList = new VertexPositionNormalTexture[POINTLISTINDEXSIZE];
            lineListIndices = new int[POINTLISTINDEXSIZE];
            texture = Game1.bluewhite;
            int i = 0;
            Vector3 vector;
            float height = 50f;

            for (float x = -1 * Game1.GRIDBOUNDRY * 2; x <= Game1.GRIDBOUNDRY * 2; x += Game1.SQUARESIZE)
            {
                vector = new Vector3(x, height, -1 * Game1.GRIDBOUNDRY * 2);
                pointList[i++] = new VertexPositionNormalTexture(vector, Vector3.Forward, Vector2.One);
                vector = new Vector3(x, height, Game1.GRIDBOUNDRY * 2);
                pointList[i++] = new VertexPositionNormalTexture(vector, Vector3.Forward, Vector2.One);
            }
            for (float z = -1 * Game1.GRIDBOUNDRY * 2; z <= Game1.GRIDBOUNDRY * 2; z += Game1.SQUARESIZE)
            {
                vector = new Vector3(-1 * Game1.GRIDBOUNDRY * 2, height, z);
                pointList[i++] = new VertexPositionNormalTexture(vector, Vector3.Forward, Vector2.One);
                vector = new Vector3(Game1.GRIDBOUNDRY * 2, height, z);
                pointList[i++] = new VertexPositionNormalTexture(vector, Vector3.Forward, Vector2.One);
            }

            for (int y = 0; y < POINTLISTINDEXSIZE; y++)
            {
                lineListIndices[y] = y;
            }

            //Vertex Declaration
            vertexDeclaration = new VertexDeclaration(Game1.Graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
        }

        public void Update()
        {


        }

        public void Draw()
        {
            Game1.Graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
            Game1.basicEffect.Begin();
            Game1.basicEffect.Texture = texture;
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();

                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.LineList, pointList, 0, POINTLISTINDEXSIZE, lineListIndices, 0, POINTLISTINDEXSIZE / 2);

                pass.End();
            }
            Game1.basicEffect.End();
        }

    }
}